package com.tmq.diancan.controller;

import com.tmq.diancan.entity.DiancanShareImg;
import com.tmq.diancan.service.*;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * @Author tangmqb DCITS
 * @DATE 2021/3/14 14:46
 * @Version 1.0.0
 */
@RestController
@AllArgsConstructor
@RequestMapping("/wx/home")
@Api(value = "接口", tags = "接口")
public class DiancanWxIndexController {
    private IDiancanBannerService diancanBannerService;
    private IDiancanNoticeService diancanNoticeService;
    private IDiancanProductCategoryService diancanProductCategoryService;
    private IDiancanProductInfoService diancanProductInfoService;
    private IDiancanHdService diancanHdService;
    private IDiancanShareImgService diancanShareImgService;

    @RequestMapping("/index")
    public R wxindex() {
        ExecutorService executorService = Executors.newFixedThreadPool(5);

        Callable<List> bannerListCallable = () -> diancanBannerService.list();
        Callable<List> noticeListCallable = () -> diancanNoticeService.list();
        Callable<List> categoryListCallable = () -> diancanProductCategoryService.list();
        Callable<List> exampleListCallable = () -> diancanProductInfoService.list();
        Callable<List> hdListCallable = () -> diancanHdService.listHd();
        Callable<List> shareImgListCallable = () -> diancanShareImgService.list();

        FutureTask<List> bannerTask = new FutureTask<>(bannerListCallable);
        FutureTask<List> noticeTask = new FutureTask<>(noticeListCallable);
        FutureTask<List> categoryTask = new FutureTask<>(categoryListCallable);
        FutureTask<List> exampleTask = new FutureTask<>(exampleListCallable);
        FutureTask<List> hdTask = new FutureTask<>(hdListCallable);
        FutureTask<List> shareImgTask = new FutureTask<>(shareImgListCallable);

        executorService.submit(bannerTask);
        executorService.submit(noticeTask);
        executorService.submit(categoryTask);
        executorService.submit(exampleTask);
        executorService.submit(hdTask);
        executorService.submit(shareImgTask);
        Map<String, Object> entity = new HashMap<>();

        try {
            entity.put("bannerList", bannerTask.get());
            entity.put("noticeList", noticeTask.get());
            entity.put("categoryList", categoryTask.get());
            entity.put("exampleList", exampleTask.get());
            entity.put("hdList", hdTask.get());
            entity.put("shareImgList", shareImgTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return R.data(entity);
    }
}
